package com.gzs.learn.payment.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.gzs.learn.inf.PageResponseDto;
import com.gzs.learn.inf.PageResponseDto.PageResponse;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;

/**
 * 表格数据处理
 *
 * @author guanzhisong
 */

public class PageUtil {
	private static final int MAX_RECORDS_LIMIT = 5000;

	/**
	 * 默认的最大页面参数
	 *
	 * @return
	 */
	public static Page<?> getMaxPageParam() {
		return new Page<>(1, MAX_RECORDS_LIMIT);
	}

	public static <T> PageResponseDto<T> buildPageResponse(IPage<T> pageInfo) {
		if (pageInfo.getTotal() == 0) {
			return PageResponseDto.success(Lists.newArrayList(), PageResponse.getNullPage());
		}
		List<T> records = pageInfo.getRecords();
		PageResponse page = PageResponse.builder().page((int) pageInfo.getCurrent()).pageSize((int) pageInfo.getSize())
				.total((int) pageInfo.getTotal()).build();
		return PageResponseDto.success(records, page);
	}

	public static <T> PageResponseDto<T> buildPageResponse(List<T> records, int total, Page<?> p) {
		if (CollectionUtils.isEmpty(records)) {
			return PageResponseDto.success(Lists.newArrayList(), PageResponse.getNullPage());
		}
		PageResponse page = PageResponse.builder().page((int) p.getCurrent()).pageSize((int) p.getSize()).total(total)
				.build();
		return PageResponseDto.success(records, page);
	}
}
